package com.kingtsoft.study.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.kingtsoft.study.common.JsonResult;
import com.kingtsoft.study.constant.DescribableEnum;
import com.kingtsoft.study.model.StudyUser;
import com.kingtsoft.study.webserivce.WebLoginService;

import java.util.List;

@Controller
@RequestMapping("/login")
public class LoginController extends MultiActionController {

    @Autowired
    private WebLoginService loginService;

    /**
     * 跳转到主页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoMain", method = RequestMethod.GET)
    public ModelAndView gotoMain() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("username", loginService.getLoginName());
        mv.setViewName("main/main");
        return mv;
    }

    /**
     * @param request
     * @param response
     * @param studyUser
     * @return
     * @Title: doLogin
     * @Description: 用户登录
     * @author: shenlx
     * @return: Object
     */
    @RequestMapping("/doLogin")
    @ResponseBody
    public Object doLogin(HttpServletRequest request, HttpServletResponse response, StudyUser studyUser) {

        return loginService.doLogin(studyUser);
    }

    /**
     * @param request
     * @return
     * @Title: register
     * @Description: 打开注册页面
     * @author: shenlx
     * @return: ModelAndView
     */
    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public ModelAndView register(HttpServletRequest request) {

        ModelAndView mv = new ModelAndView();
        mv.setViewName("login/register");
        return mv;
    }

    /**
     * @param request
     * @param response
     * @param username
     * @return
     * @Title: remoteUsername
     * @Description: 校验用户名唯一性
     * @author: shenlx
     * @return: Object
     */
    @RequestMapping("/remoteUsername")
    @ResponseBody
    public Object remoteUsername(HttpServletRequest request, HttpServletResponse response, String username) {
        return loginService.remoteUsername(username);
    }

    /**
     * @param request
     * @param response
     * @param studyUser
     * @param studentName 这里为了方便，只做了学生注册，并只保存学生姓名
     * @return
     * @Title: addRegister
     * @Description: 用户注册
     * @author: shenlx
     * @return: Object
     */
    @RequestMapping("/addRegister")
    @ResponseBody
    public Object addRegister(HttpServletRequest request, HttpServletResponse response, StudyUser studyUser, String studentName) {
        loginService.addRegister(studyUser, studentName);
        return new JsonResult(DescribableEnum.SUCCESS);
    }

    /**
     * @param request
     * @return
     * @Title: doLogout
     * @Description: 登出
     * @return: ModelAndView
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public ModelAndView doLogout(HttpServletRequest request) {
        try {
            loginService.doLogout();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ModelAndView("redirect:/");
    }

    /**
     * 获取菜单
     * @return
     * @author jerry.pan
     * @date 2017-04-12 10:04:33
     */
    @RequestMapping(value = "/menu")
    @ResponseBody
    public JsonResult menu() {
        List list = loginService.menu();
        return new JsonResult(DescribableEnum.SUCCESS, list);
    }

}
